//
//  ViewController.swift
//  01Demo
//
//  Created by Clover on 2018/4/13.
//  Copyright © 2018年 youbing. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var resetBtn: UIButton!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var stop: UIButton!
    @IBOutlet weak var star: UIButton!
    
    var timer = Timer()
    var count = 0.0
    var isPalying = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        timeLabel.text = String(count)
    }

    @IBAction func reset(_ sender: Any) {
        timeLabel.text = "0.0"
        count = 0.0
        isPalying = false
        timer.invalidate()
    }
    
    @IBAction func star(_ sender: UIButton) {
//        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true)
        if isPalying {
            return
        }else{
            timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true)
            isPalying = true
        }
    }
    
    @IBAction func stop(_ sender: Any) {
        if !isPalying {
            return
        }
        isPalying = false
        timer.invalidate()
    }
    
    @objc func updateTimer() {
        count += 0.1
        timeLabel.text = String(format: "%.1f", count)
    }
    
}

